package com.cn.nullframe.service;

import com.cn.nullframe.mapper.UserMapper;
import com.cn.nullframe.pojo.User;
import com.cn.nullframe.service.base.BaseService;
import com.cn.nullframe.service.base.CommonService;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService extends BaseService<User> {

    @Autowired
    private UserMapper userMapper;


    /**
     * 测试分页插件
     *
     * @param page
     * @param rows
     * @return
     */
    public PageInfo<User> queryPageList(String name, Integer page, Integer rows) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        example.setOrderByClause("updated DESC");
        // 设置分页参数 一定要放在查询前面
        /**
         * 最终执行的sql:
         SELECT ID,NAME,MOBILE,PHONE,PASSWORD,EMAIL,CREATED,UPDATED FROM user WHERE ( NAME = ? ) order by updated DESC LIMIT ?
         */
        PageHelper.startPage(page, rows);
        List<User> list = userMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    /**
     * 测试事物是否生效
     */
    public String testTtransaction() {
        User user = new User();
        user.setName("111111");
        user.setId(1l);
        userMapper.updateByPrimaryKey(user);
        int i = 1 / 0;
        return "123";
    }

    public void testUser() {
        User user = new User();
        userMapper.insert(user);
        int i = 1 / 0;
    }
}
